<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午5:48
 */

namespace app\index\controller;

use think\Controller;


/* APP 管理 */
class AppManager extends Controller{

    //判断用户是否登录
    /*public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {
            $this->error('对不起,您还没有登录!请先登录!','index/login');

        }

    }*/

    /* app列表
        表： go_shoplist  prefix:go_
        app 端 专用 只显示 启用的 产品
    */
    public function applist(){


        //参数接收
//        $title = input('get.title');    //app界面搜索似乎意义不大，暂时去掉
        /*
         * type 用来判断是根据什么来区分
         * 0.为推荐优选（首页显示）  order
         * 1.最高额度从高到低排序     maxmoney
         * 2.按利率从低到高排序      minfeilv
         * 3.按贷款期限 从长到短排序   maxdate
         * */

        $ip = input('get.ip');  //游客 或 会员的 ip 地址 用来统计 uv
        $type = input('get.type');
        $page = input('get.page');  //页数
        $lines = input('get.lines'); //行数

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;


        //入库统计信息
        db('tongji')->insert(['ip'=>$ip,'time'=>time(),'date'=>date('Y-m-d')]);

        $where = [];
        $order = '';  //排序 变量初始化
        if(!empty($type)  &&  is_numeric($type) || $type === 0){
            switch ($type){
                case 0: $order = 'order desc';break;
                case 1: $order = 'maxmoney desc';break;
                case 2: $order = 'minfeilv asc';break;
                case 3: $order = 'maxdate desc';break;
                default: $order = 'id desc';break;  //默认按 id 倒序排，也就是默认 新添加的 会显示在前面

            }
        }

        $where['state'] = 1; // 1 为启用状态  必须

        if($where){
            $res = db('shoplist')->where($where)->order($order)->select();

        }else{
            $res = db('shoplist')->order($order)->select();
        }

        foreach($res as $k=>&$v){
            $v['thumb'] = 'http://wyq.weikj.net:86/uploads/' . $v['thumb'];  //拼接logo 图片地址
        }

        $top = $lines;
        if(count($res) < $top){
            $top = count($res);
        }

        $sbid = ($page - 1) * $lines;

        $tmp = [];
        for($i=0;$i<$top;$i++){
            if(!isset($res[$sbid])) break; //防止 未定义数组下标
            $tmp[$i] = $res[$sbid];
            $sbid+=1;
        }

        $msg = [];//初始化  消息盒子
        if(!empty($tmp) && $tmp!==false){
            $msg['rows'] = $tmp;
            $msg['total'] = count($res);
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['result'] = 1;
            $msg['total'] = 0;
            $msg['tips'] = '查询成功！但是并没有数据哟~';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        else{
            $msg['rows'] = [];
            $msg['result'] = 2;
            $msg['tips'] = '查询失败！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }


    }//applist


    /* app列表
        表： go_shoplist  prefix:go_
        后台专用， 显示 启用 & 禁用的产品
    */
    public function applist_admin(){


        //参数接收
        $title = input('get.title');    //app名称
        $page = input('get.page');  //页数
        $lines = input('get.lines'); //行数

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;



        $where = [];
        if($title){
            $title = trim($title);
            $where['title'] = ['LIKE' , '%'.$title.'%'];
        }

        if($where){
            $res = db('shoplist')->where($where)->order('order desc')->select();


        }else{
            $res = db('shoplist')->order('order desc')->select();
        }

        foreach($res as $k=>&$v){
            $v['thumb'] = 'http://wyq.weikj.net:86/uploads/' . $v['thumb'];  //拼接logo 图片地址
        }

        $top = $lines;
        if(count($res) < $top){
            $top = count($res);
        }

        $sbid = ($page - 1) * $lines;
//        echo $sbid;die;
        $tmp = [];
        for($i=0;$i<$top;$i++){
            if(!isset($res[$sbid])) break; //防止 未定义数组下标
            $tmp[$i] = $res[$sbid];
            $sbid+=1;
        }

        $msg = [];//初始化  消息盒子
        if(!empty($tmp) && $tmp!==false){
            $msg['rows'] = $tmp;
            $msg['total'] = count($res);
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['result'] = 1;
            $msg['total'] = 0;
            $msg['tips'] = '查询成功！但是并没有数据哟~';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        else{
            $msg['rows'] = [];
            $msg['result'] = 2;
            $msg['tips'] = '查询失败！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }


    }


    //app 排序
    public function apprange(){
        $id = input('post.id'); //要修改的 id
        $order = input('post.order');  //排序

        $where = [];
        if($order){
            $where['order'] = $order;
        }

        if($id){
            $where['id'] = $id;
        }

        $box = [];
        if(!empty($where)){

            $res = db('shoplist')->where(['id'=>$id])->update(['order'=>$order]);
            if($res!==false){
                $box['result'] =1;
                $box = json_encode($box,320);
                exit($box);
            }
        }else{
            $box['result'] =2;
            $box['tips'] = '无法修改商品排序';
            $box = json_encode($box,320);
            exit($box);
        }

    }//apprange func

    /* app添加  */
    public function appAdd(){
        //参数接收
        $data = [];

//        dump($_FILES['logo']['tmp_name']);die;
        if(!empty($_FILES['logo']['tmp_name'])){
            $data['thumb'] = $this->upload();
        }else{
            $data['thumb'] = 'photo/goods.jpg';
        }
        $data['title'] = input('post.title');//APP标题
        $data['title2'] = input('post.title2');//APP简介
        $data['money'] = input('post.money');//额度范围
        $data['maxmoney'] = input('post.maxmoney'); //最大金额
        $data['jiage'] = input('post.jiage'); //日费率
        $data['minfeilv'] = input('post.minfeilv'); //最小费率
        $data['yunjiage'] = input('post.yunjiage');  //贷款期限
        $data['maxdate'] = input('post.maxdate');  //最长期限
        $data['zongrenshu'] = input('post.zongrenshu');   //参与人数
        $data['content'] = input('post.content');   //链接
        $data['is_mj'] = input('post.is_mj');   //是否马甲包  0.正式包 ； 1.马甲包
        $data['uv_day'] = input('post.uv_day');   //每日uv
        $data['on_shell_time'] = input('post.on_shell_time');   //上线时间
        $data['off_shell_time'] = input('post.off_shell_time');   //上xian时间
        $data['price'] = input('post.price');   //单价
        $data['sid'] = 0 ;  //默认是0  不知道这是啥意思  同一款产品的标识..
        $data['q_user'] = ''; //中奖人信息...  数据库是必填

        $box = [];
        if($data){
            $res = db('shoplist')->insert($data);
            if($res){
                $box['result'] =1;
                $box['tips'] = 'app添加成功！!';
                $box = json_encode($box,320);

            }
            else{

                $box['result'] =2;
                $box['tips'] = 'app添加失败！!';
                $box = json_encode($box,320);

            }

        }//if
        else{
            $box['result'] =2;
            $box['tips'] = '请填写了数据再提交吧~^_^!';
            $box = json_encode($box,320);
        }
        exit($box);


    }//func add


    /* app信息修改*/
    public function appModify(){


        $data = [];

        //判断是否更新图片
        if(!empty($_FILES['logo']['tmp_name'])){
            $data['thumb'] = $this->upload();
        }


        //接收参数
        $data['id'] = input('post.id');//APP标题
        $data['title'] = input('post.title');//APP标题
        $data['title2'] = input('post.title2');//APP简介
        $data['money'] = input('post.money');//额度范围
        $data['maxmoney'] = input('post.maxmoney'); //最大金额
        $data['jiage'] = input('post.jiage'); //日费率
        $data['minfeilv'] = input('post.minfeilv'); //最小费率
        $data['yunjiage'] = input('post.yunjiage');  //贷款期限
        $data['maxdate'] = input('post.maxdate');  //最长期限
        $data['zongrenshu'] = input('post.zongrenshu');   //参与人数
        $data['content'] = input('post.content');   //链接
        $data['is_mj'] = input('post.is_mj');   //是否马甲包  0.正式包 ； 1.马甲包
        $data['uv_day'] = input('post.uv_day');   //每日uv
        $data['on_shell_time'] = input('post.on_shell_time');   //上线时间
        $data['off_shell_time'] = input('post.off_shell_time');   //上xian时间
        $data['price'] = input('post.price');   //单价
        $data['sid'] = 0 ;  //默认是0  不知道这是啥意思  同一款产品的标识..
        $data['q_user'] = ''; //中奖人信息...  数据库是必填

//        dump($data);die;

//        $data = input('post.');
        $id = $data['id'];
        unset($data['id']);//去掉id




        //更新数据
        $res = db('shoplist')->where(['id'=>$id])->update($data);

        $box=[];
        if($res !==false){

            $box['result'] =1;
            $box['tips'] = 'APP信息更新成功！~^_^!';
            $box = json_encode($box,320);

            // $this->success('APP信息更新成功！',usrl('appList'),'',2);
        }
        else{
            $box['result'] =2;
            $box['tips'] = 'APP信息更新失败！~^_^!';
            $box = json_encode($box,320);

        }

        exit($box);

    }//func app信息修改


    /*app信息删除  软删除*/
    public function delApp(){
        $id = input('post.id');
        db('shoplist')->delete($id);
        $box['result'] =1;
        $box['tips'] = 'APP删除成功！';
        $box = json_encode($box,320);
        exit($box);

    }


    /* 马甲包列表  */
    public function packageList(){

        //接收参数
        $title = input('get.title');
        $page = input('get.page');  //页数
        $lines = input('get.lines'); //行数

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;


        $where = [];
        if($title){
            $title = trim($title);
            $where['title'] = $title;
        }
        $where['is_mj'] = 1;
        $res = db('shoplist')->where($where)->select();

        foreach($res as $k=>&$v){
            $v['thumb'] = '/uploads/' . $v['thumb'];  //拼接logo 图片地址
        }

        
        $box = [];
        $tmp=[];
        if($res && $res!==false){
            $sbid = ($page - 1) * $lines;

            for($i=0;$i<$lines;$i++){
                if(isset($res[$sbid])){
                    $tmp[] =  $res[$sbid];
                }
                else{
                    break;
                }
                $sbid += 1;

            }

            $tmp = array_values($tmp);//确保下标按顺序从0顺序排

            $box['rows'] = $tmp;
            $box['total'] = count($res);
            $box['result'] =1;
            $box['tips'] = '信息查询成功！~^_^!';
            $box = json_encode($box,320);

        }
        elseif(empty($res)){
            $box['rows'] = $tmp;
            $box['total'] = 0;
            $box['result'] =1;
            $box['tips'] = '查询成功！  暂时没有数据哟~~！';
            $box = json_encode($box,320);

        }
        else{
            $box['result'] =2;
            $box['tips'] = '查询失败！ 请稍后重试';
            $box = json_encode($box,320);

        }
        exit($box);
        // $this->assign('mjlist',$res);
    }



    /*app停用启用 */
    public function app_start_stop(){
        $id = input('post.id');
        $state = input('post.state/d');  //1.启用， 2.停用

        //状态 相反 着 更新 才能起到 开关的作用
        switch ($state){
            case 1:$state=2;break;
            case 2:$state=1;break;
            default:$state=2;break;  //默认关闭
        }

        $res = db('shoplist')->where(['id'=>$id])->update(['state'=>$state]);

        if($res !==false){
            //成功更新
            $box['result'] =1;
            $box['tips'] = 'OK!';
            $box = json_encode($box,320);


        }else{
            $box['result'] =2;
            $box['tips'] = '网络连接失败';
            $box = json_encode($box,320);


        }

        exit($box);


    }//func

    /*  用户 对商品点击 的统计 */
    public function click_statistic(){

        $uid = input('post.uid');
        $ip = input('post.ip');
        $shopid = input('post.shopid');


        /*点击统计*/
        $where = [];
        $where['uid'] = $uid;
        $where['ip'] = $ip;
        // $where['shopid'] = $shopid;
        $where['time'] = time();
        $where['date'] = date('Y-m-d');
        db('tongji')->insert($where);

        $data  = [];
        $data['uid'] = trim($uid);
        $data['ip'] = trim($ip);
        $data['shopid'] = trim($shopid);
        // $data['datetime'] = time(); //时间戳
        $data['date'] = date('Y-m-d');
        $data['cishu'] = 1;
        $res = db('shop_dj')->insert($data);
        $box = [];
        if(!$res){

            $box['result'] =2;
            $box['tips'] = '网络连接失败!';
            $box = json_encode($box,320);
            exit($box);
        }
        else{
            $box['result'] =1;
            $box['tips'] = '成功收集用户点击信息!';
            $box = json_encode($box,320);
            exit($box);
        }
    }


    /*图片上传类*/
    public function upload(){


        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('logo');


        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            //  /Library/WebServer/Documents/douhua/public/uploads/shopimg
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'shopimg');
//            dump($info);die;
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                /*echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();*/

                return 'shopimg/' . $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }//else
        }//if
    }//func upload



    /* app 顺序 重排 */

    public function re_rank(){
        $res = db('shoplist')->select();

        $count = 1;
        foreach($res as &$v){
            $v['order'] = $count;
            $count +=1;
        }

        foreach($res as $v){
            db('shoplist')->where(['id'=>$v['id']])->update($v);
        }

    }






}